import { Component, OnInit } from '@angular/core';
import {Course, CourseInfo, Student, Studentcourse, Studentgrade} from '../student';
import {HttpService} from '../http.service';

@Component({
  selector: 'app-studentsgrade',
  templateUrl: './studentsgrade.component.html',
  styleUrls: ['./studentsgrade.component.css']
})
export class StudentsgradeComponent implements OnInit {
  students: Student[] = [];
  loading = true;
  nzPageSize = 10;
  courseinfo: Course[] = [];
  studentcourses: Studentcourse[] = [];
  studentgrade: Studentgrade[] = [];
  searchValue = '';
  searchValue2 = '';
  visible = false;
  visible2 = false;
  DisplayData: any ;
  DisplayData2: any ;
  id: number;
  private courseDataPlay: CourseInfo[];
  courseData: CourseInfo[] = [];
  private courseinfoData: Course[];
  private studentsData: Student[];
  constructor(private httpService: HttpService) { }
  reset() {
    this.DisplayData = this.DisplayData2;
  }
  search() {
    this.visible = false;
    this.DisplayData = this.DisplayData.filter((item: Studentgrade) => item.major.indexOf(this.searchValue) !== -1) ;
  }
  search2() {
    this.visible2 = false;
    this.DisplayData = this.DisplayData.filter((item: Studentgrade) => item.class.indexOf(this.searchValue2) !== -1) ;
  }
  getcouresinfo() {
    return this.httpService.course_select1().subscribe(
      (res) => {
        res.data.forEach(cou => {
          this.studentgrade = [...this.studentgrade];
          this.studentgrade.push({
            Courseid: cou.courseid,
            class: this.getClass(cou.stuid),
            coursename: this.getCourseName(cou.courseid),
            credit: this.getCredit(cou.courseid),
            gradepoint: cou.score/10-5,
            major: this.getMajor(cou.stuid),
            type: this.getType(cou.courseid),
            score: cou.score,
            stuid: cou.stuid,
          });
          this.DisplayData = this.studentgrade;
          this.DisplayData2 = this.studentgrade;
          this.loading = false;
        });
        this.courseinfoData = this.courseinfo;
        console.log(this.studentgrade);
        // for ( const i in this.courseinfo) {
        //   this.httpService.getCourseDetail(this.courseinfo[i].courseid).subscribe(
        //     result => {
        //       this.studentcourses.push({
        //         Courseid: result.data.courseid,
        //         coursename: result.data.coursename,
        //         stuid: this.courseinfo[i].stuid,
        //         credit: result.data.credit,
        //         type: result.data.type,
        //         score: this.courseinfo[i].score,
        //         gradepoint: this.courseinfo[i].score / 10 - 5
        //       });
        //       console.log(this.studentcourses);
        //       for (const j in this.studentcourses) {
        //         this.httpService.getStudentDetail(this.studentcourses[j].stuid).subscribe(
        //           row => {
        //             console.log(this.courseinfo[j].courseid);
        //             this.studentgrade.push({
        //               major: row.data.major,
        //               class: row.data.clsaa,
        //               Courseid: this.studentcourses[j].Courseid,
        //               coursename: this.studentcourses[j].coursename,
        //               stuid: this.studentcourses[j].stuid,
        //               credit: this.studentcourses[j].credit,
        //               type: this.studentcourses[j].type,
        //               score: this.studentcourses[j].score,
        //               gradepoint: this.studentcourses[j].score / 10 - 5
        //             });
        //             console.log(this.studentgrade);
        //             this.DisplayData = this.studentgrade;
        //             this.DisplayData2 = this.studentgrade;
        //             this.loading = false;
        //           }
        //         );
        //       }
        //     } );
        // }
      }
    );
  }
  getClass(stuid){
    for(const i in this.studentsData){
      if(stuid === this.studentsData[i].stuid){
        return this.studentsData[i].class;
        break;
      }
    }
  }
  getCourseName(courseid){
    for(const i in this.courseDataPlay){
      if(courseid === this.courseDataPlay[i].courseid){
        return this.courseDataPlay[i].coursename;
        break;
      }
    }
  }
  getCredit(courseid){
    for(const i in this.courseDataPlay){
      if(courseid === this.courseDataPlay[i].courseid){
        return this.courseDataPlay[i].credit;
        break;
      }
    }
  }
  getMajor(stuid){
    for(const i in this.studentsData){
      if(stuid === this.studentsData[i].stuid){
        return this.studentsData[i].major;
        break;
      }
    }
  }
  getType(courseid){
    for(const i in this.courseDataPlay){
      if(courseid === this.courseDataPlay[i].courseid){
        return this.courseDataPlay[i].type;
        break;
      }
    }
  }

  getCourseData() {
    this.httpService.getCourses().subscribe(
      (res)=> {
        res.data.forEach(cou => {
          this.courseData = [...this.courseData];
          this.courseData.push({
            courseid: cou.courseid,
            coursename: cou.coursename,
            credit: cou.credit,
            type: cou.type
          });
        });
        console.log(this.courseData);
        this.courseDataPlay = this.courseData;
      });
  }
  getStudents(): void {
    this.httpService.getStudents().subscribe(
      (res) => {
        res.data.forEach(row => {
          this.students = [...this.students];
          this.students.push( {
            native_place: row.native_place,
            time_of_enrollment: row.time_of_enrollment,
            birthday : row.birthday,
            class : row.class,
            major : row.major,
            name : row.name,
            sex : row.sex,
            stuid : row.stuid
          });
        });
        this.studentsData = this.students;
        console.log(this.students);
      });
  }
  ngOnInit(): void {
    this.getCourseData();
    this.getStudents();
    this.getcouresinfo();
  }

}
